#include "h_cc2640r2f_pwm.h"

#include <ti/drivers/PWM.h>
#include <ti/drivers/pwm/PWMTimerCC26XX.h>

#include <board.h>

#include "h_cc2640r2f_define.h"
#include "h_cc2640r2f_uart.h"

static PWM_Handle H_CC2640R2F_PWMHandle0 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle1 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle2 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle3 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle4 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle5 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle6 = NULL;
static PWM_Handle H_CC2640R2F_PWMHandle7 = NULL;

static PWM_Handle *H_CC2640R2F_PWMHandleTemp = NULL;

static PWM_Params H_CC2640R2F_PWMparams;

//#define H_CC2640R2F_PWM_Hundred2MAX_Times               PWM_DUTY_FRACTION_MAX / 100

static PWM_Handle* h_cc2640r2f_pwm_findpwmhandle(unsigned char channel)                                                    
{
    switch(channel)
    {
        case CC2640R2_LAUNCHXL_PWM0:
            //return H_CC2640R2F_PWMHandle0;
            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle0;
            break;
            
//        case CC2640R2_LAUNCHXL_PWM1:
//            //return H_CC2640R2F_PWMHandle1;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle1;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM2:
//            //return H_CC2640R2F_PWMHandle2;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle2;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM3:
//            //return H_CC2640R2F_PWMHandle3;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle3;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM4:
//            //return H_CC2640R2F_PWMHandle4;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle4;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM5:
//            //return H_CC2640R2F_PWMHandle5;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle5;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM6:
//            //return H_CC2640R2F_PWMHandle6;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle6;
//            break;
//
//        case CC2640R2_LAUNCHXL_PWM7:
//            //return H_CC2640R2F_PWMHandle7;
//            H_CC2640R2F_PWMHandleTemp = &H_CC2640R2F_PWMHandle7;
//            break;
            
        default:
          H_CC2640R2F_PWMHandleTemp = NULL;
          break;
    }
    return H_CC2640R2F_PWMHandleTemp;
}

signed char H_CC2640R2F_PWM_Init(unsigned char channel, unsigned short freq)
{
    
    //PWM_Handle *H_CC2640R2F_PWMHandle = &H_CC2640R2F_PWMHandle3;
    h_cc2640r2f_pwm_findpwmhandle(channel);
    
    PWM_init();
    PWM_Params_init(&H_CC2640R2F_PWMparams);
    
    H_CC2640R2F_PWMparams.periodUnits = PWM_PERIOD_HZ;
    H_CC2640R2F_PWMparams.periodValue = freq;
    H_CC2640R2F_PWMparams.dutyUnits = PWM_DUTY_FRACTION;
    H_CC2640R2F_PWMparams.dutyValue = 0;
    H_CC2640R2F_PWMparams.idleLevel = PWM_IDLE_LOW;
    H_CC2640R2F_PWMparams.custom = NULL;
    
    *H_CC2640R2F_PWMHandleTemp = PWM_open(channel, \
                                        &H_CC2640R2F_PWMparams);
    
    if(*H_CC2640R2F_PWMHandleTemp == NULL)
    {
        return (-1);
    }
    return 0;
}

static unsigned int h_cc2640r2f_pwm_getrealdutuvalue(unsigned char value)
{
        
	float tf = 0.0f;
        //value = (value < 0)? 0 : value;
        value = (value > 100)? 100 : value;
        
        tf = value / 100.0;
	tf *= PWM_DUTY_FRACTION_MAX;
	return (unsigned int)tf;
}

void H_CC2640R2F_PWM_StartChannel(unsigned char channel)
{
    PWM_start(*h_cc2640r2f_pwm_findpwmhandle(channel));
}

void H_CC2640R2F_PWM_StopChannel(unsigned char channel)
{
    PWM_stop(*h_cc2640r2f_pwm_findpwmhandle(channel));
}

void H_CC2640R2F_PWM_ChannelSetDuty(unsigned char channel, \
                                                unsigned char duty)
{        
    PWM_setDuty(*h_cc2640r2f_pwm_findpwmhandle(channel), \
                        h_cc2640r2f_pwm_getrealdutuvalue(duty));
}


//void H_CC2640R2F_PWM_Test(void)
//{
//    unsigned char i = 0;
////    signed char flag = 1;
//
//
//    //unsigned int pwm = 0;
//
//
//    H_CC2640R2F_PWM_Init(CC2640R2_LAUNCHXL_PWM2, 1024);
//    //H_CC2640R2F_PWM_ChannelSetDuty(CC2640R2_LAUNCHXL_PWM2, 50);
//    //H_CC2640R2F_PWM_StartChannel(CC2640R2_LAUNCHXL_PWM2);
//
//
//
//    while(1)
//    {
//        if(i <= 1)
//        {
//            flag = 1;
//        }
//        if(i >= 100)
//        {
//            flag = -1;
//        }
//
//
//        //H_CC2640R2F_PWM_ChannelSetDuty(CC2640R2_LAUNCHXL_PWM2, i);
//    }
//}


